﻿using Framework.Core.Extensions;
using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.DependencyInjection;

namespace Framework.AspNetCore.Middlewares
{
	/// <summary>
	/// API 信息 中间件
	/// </summary>
	public class ApiInfoMiddleware : IMiddleware, ITransientDependency
	{
		public async Task InvokeAsync(HttpContext context, RequestDelegate next)
		{
			context.Response.OnStarting(() =>
			{
				if (context.Response.StatusCode == StatusCodes.Status200OK
&& context.Response.Headers["Content-Type"].ToString() == "application/vnd.ms-excel")   // excel文件正常返回时，设置返回的文件名为当前时间
				{
					context.FileAttachmentHandle($"{DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss")}.xlsx");   // 设置文件附件名称
				}
				return Task.CompletedTask;
			});

			await next(context);



		}
	}
}
